﻿namespace EasyGo.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;

    using EasyGo.Authentication;
    using EasyGo.Models;

    using Resources;

    public class AccountController : Controller
    {
        private readonly IAuthenticationService auth;

        public AccountController(IAuthenticationService auth)
        {
            this.auth = auth;
        }

        [HttpGet]
        public virtual ActionResult Login()
        {
            return this.View(new Login());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Login(Login model)
        {
            if (ModelState.IsValid)
            {
                if (this.auth.TrySignIn(model.Email, model.Password, model.RememberMe))
                {
                    return this.RedirectToAction("Index", "Home");
                }

                this.ModelState.AddModelError(string.Empty, Loc.IncorrectLogin);
            }
            return this.View(model);
        }

        public ActionResult LogOut()
        {
            this.auth.SignOut();
            return this.RedirectToAction("Index", "Home");
        }
    }
}